package com.beamfield.controller.manage.operator;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.dictionary.code;
import com.beamfield.model.SpecialOperator;
import com.beamfield.model.SystemDictionaryData;
import com.beamfield.modelVo.SystemDictionaryDataVo;
import com.beamfield.service.manage.sys.DictonaryService;
import com.beamfield.service.manage.sys.FileService;
import com.beamfield.service.manage.operator.ManagerInfoService;
/**
 * 特殊作业人员
 * @author zhaohh
 *
 */
@Controller
@RequestMapping("/cms/operator/managerInfo")
public class SpecialOperatorController {
	
	@Value("${session.user}")
	private String user;
	
	@Value("${session.beam}")
	private String beam;

	@Resource
	ManagerInfoService managerInfoService;
	
	@Resource
	FileService fileService;
	
	@Resource
	DictonaryService dictonaryService;
	
	/**
	 * 人员管理页面
	 * @return
	 */
	@RequestMapping("/specialOperator")
	public ModelAndView specialOperator(){
		return new ModelAndView();
	}
	/**
	 * 获取管理人员列表
	 * @param offset
	 * @param limit
	 * @param ecode
	 * @param name
	 * @return
	 */
	@RequestMapping("/getSpecialOperatorList")
	@ResponseBody
	public PageResult getSpecialOperatorList(Integer offset, Integer limit,String ecode,String name,String presasg, String work, HttpSession session){
		Long beam_id = (Long) session.getAttribute(beam);
		return managerInfoService.getSpecialOperatorList( offset,  limit, ecode, name,beam_id, presasg, work);
	}
	
	/**
	 * 编辑或新增页面
	 * @return
	 */
	@RequestMapping("/editSpecialOperator")
	public ModelAndView editSpecialOperator(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	@RequestMapping("/getSpecialOperatorEditInfo")
	@ResponseBody
	public ApiResult getSpecialOperatorEditInfo(Long id,HttpSession session){
		ApiResult apiResult = new ApiResult();
		long beam_id = (long) session.getAttribute(beam);
		Map<String, Object> map = new HashMap<String, Object>();
		SpecialOperator specialOperator = managerInfoService.getSpecialOperatorById(id);
		if(specialOperator != null){
			specialOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(specialOperator.getAvatar())));
			if(specialOperator.getQrcode() != null){
				specialOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(specialOperator.getQrcode())));
			}
		}
		List<SystemDictionaryDataVo> department = dictonaryService.getDicByCode(code.department,beam_id);
		List<SystemDictionaryDataVo> health = dictonaryService.getDicByCode(code.health,beam_id);
		List<SystemDictionaryDataVo> sex = dictonaryService.getDicByCode(code.sex,beam_id);
		List<SystemDictionaryDataVo> group = dictonaryService.getDicByCode(code.group,beam_id);
		List<SystemDictionaryDataVo> work_type = dictonaryService.getDicByCode(code.special_work,beam_id);
		map.put("work_type", work_type);
		map.put("department", department);
		map.put("health", health);
		map.put("sex", sex);
		map.put("group", group);
		map.put("specialOperator", specialOperator);
		apiResult.setResult(map);
		return apiResult;
	}
	
	/**
	 * 详情页面
	 * @return
	 */
	@RequestMapping("/specialOperatorDetail")
	public ModelAndView specialOperatorDetail(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	/**
	 * 获取详情数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/getSpecialOperatorDetailInfo")
	@ResponseBody
	public ApiResult getSpecialOperatorDetailInfo(Long id,HttpSession session){
		ApiResult apiResult = new ApiResult();
		long beam_id = (long) session.getAttribute(beam);
		Map<String, Object> map = new HashMap<String, Object>();
		SpecialOperator specialOperator = managerInfoService.getSpecialOperatorById(id);
		if(specialOperator != null){
			if(specialOperator.getAvatar() != null){
				specialOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(specialOperator.getAvatar())));
			}

			SystemDictionaryData sex = managerInfoService.selectByCode(specialOperator.getGender(),beam_id);
			
			if(sex != null){
				specialOperator.setGender(sex.getName());
			}
			
			SystemDictionaryData workdept = managerInfoService.selectByCode(specialOperator.getWorkdept(),beam_id);
			
			if(workdept != null){
				specialOperator.setWorkdept(workdept.getName());
			}
			
			SystemDictionaryData group = managerInfoService.selectByCode(specialOperator.getGroup(),beam_id);
			
			if(group != null){
				specialOperator.setGroup(group.getName());
			}
			
			SystemDictionaryData work_type = managerInfoService.selectByCode(specialOperator.getWork_type(),beam_id);
			
			if(work_type != null){
				specialOperator.setWork_type(work_type.getName());
			}
			
			if(specialOperator.getQrcode() != null){
				specialOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(specialOperator.getQrcode())));
			}
			SystemDictionaryData relation = managerInfoService.selectByCode(specialOperator.getRelation(),beam_id);

			if(relation != null){
				specialOperator.setRelation(relation.getName());
			}
			SystemDictionaryData health = managerInfoService.selectByCode(specialOperator.getHealth_condition(),beam_id);

			if(health != null){
				specialOperator.setHealth_condition(health.getName());
			}
		}
		map.put("specialOperator", specialOperator);
		apiResult.setResult(map);
		return apiResult;
	}
	
	
	/**
	 * 新增或编辑
	 * @return
	 */
	@RequestMapping("/doEditSpecialOperator")
	@ResponseBody
	public ApiResult doEditSpecialOperator(SpecialOperator specialOperator,HttpSession session){
		Long user_id = (Long) session.getAttribute(user);
		Long beam_id = (Long) session.getAttribute(beam);
		
		specialOperator.setUpdater(user_id);
		specialOperator.setUpdatetime(new Date());
		specialOperator.setBeam_id(beam_id);

		Integer countIDCard = managerInfoService.checkCountManagerInfoIDCard(specialOperator.getIdentity(), specialOperator.getSpecial_operator_id());
		Integer countIDCard2 = managerInfoService.checkCountNormalOperatorIDCard(specialOperator.getIdentity(), specialOperator.getSpecial_operator_id());
		Integer countIDCard3 = managerInfoService.checkCountSpecialOperatorIDCard(specialOperator.getIdentity(), specialOperator.getSpecial_operator_id());
		Integer namenum = managerInfoService.checkCountManagerInfoName(specialOperator.getName(), specialOperator.getSpecial_operator_id(),beam_id);
		if (namenum > 0) {
			return new ApiResult("1", "姓名重复！");
		}
		if (countIDCard+countIDCard2+countIDCard3 > 0) {
			return new ApiResult("1", "身份证号重复！");
		}
		
		try {
			//新增
			if(specialOperator.getSpecial_operator_id() == null){
				Integer count = managerInfoService.getSpecialOperatorCount();
				DecimalFormat format = new DecimalFormat("0000");
				String code = format.format(count+1);
				specialOperator.setEcode("02"+code);
				specialOperator.setOperator(user_id);
				specialOperator.setAddtime(new Date());
				specialOperator.setDelflag(0);
				specialOperator.setAdjust(0);
				managerInfoService.insertSpecialOperator(specialOperator);
			}
			//编辑
			if(specialOperator.getSpecial_operator_id() != null){
				managerInfoService.updateSpecialOperator(specialOperator);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult("1", "服务器异常！");
		}
		
		return new ApiResult("2", "成功！");
	}
	
	/**
	 * 删除
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("/delSpecialOperator")
	@ResponseBody
	public ApiResult delSpecialOperator(Long id,HttpSession session){
		SpecialOperator specialOperator = managerInfoService.getSpecialOperatorById(id);
		if(specialOperator == null){
			return new ApiResult("1","删除失败，查不到该数据!");
		}
		Long user_id = (Long) session.getAttribute(user);
		specialOperator.setUpdater(user_id);
		specialOperator.setUpdatetime(new Date());
		specialOperator.setDelflag(1);
		try {
			managerInfoService.updateSpecialOperator(specialOperator);
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult("1","删除失败，服务器异常!");
		}
		return new ApiResult("2","成功!");
	}
	
	/**
	 * 特种作业的培训列表
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getDetailSpecialOperatorList")
	@ResponseBody
	public PageResult getDetailSpecialOperatorList(Integer offset, Integer limit,Long special_operator_id,HttpSession session){
		return managerInfoService.getDetailSpecialOperatorList( offset,  limit, special_operator_id);
	}
	
	/**
	 * 导出数据
	 */
	@RequestMapping("/exportSpecialOperatorDetail")
	@ResponseBody
	public String exportSpecialOperatorDetail(HttpServletResponse response,String ecode,String group,String name, String presasg, HttpSession session) throws Exception{
		Long beam_id = (Long) session.getAttribute(beam);
		String apiResult = managerInfoService.exportSpecialOperatorDetail(presasg, ecode,response,name,beam_id,group);
		return apiResult;
	}
	
	/**
	 * 考勤页面
	 * @return
	 */
	@RequestMapping("/specialOperatorAttend")
	public ModelAndView specialOperatorAttend(){
		ModelAndView modelAndView = new ModelAndView();
		return modelAndView;
	}
	
	/**
	 * 考勤列表
	 * @param offset
	 * @param limit
	 * @param name
	 * @return
	 */
	@RequestMapping("/getSpecialOperatorAttendList")
	@ResponseBody
	public PageResult getSpecialOperatorAttendList(Integer offset, Integer limit,String name,String time,String group,HttpSession session){
		Long beam_id = (Long) session.getAttribute(beam);
		return managerInfoService.getSpecialOperatorAttendList( offset,limit,name,time,group,beam_id);
	}
}
